package nl.haemers.loaders
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.HTTPStatusEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;	 
	/**
	 * @author Jonathan Haemers
	 * @company Idreams
	 */
	public class VideoLoader extends EventDispatcher
	{
		private var _img			:Bitmap;
		private var _imgs			:Vector.<Bitmap>;
		private var vidLoader		:Loader;
		private var req 			:URLRequest;
		
		private var _queue			:Vector.<URLRequest>;
		
		private var _multiLoad		:Boolean;
		private var _urls			:Vector.<String>;
		private var _requests		:Vector.<URLRequest>;
		
		private var loaderInfo		:LoaderInfo;
		/**
		 * 
		 */
		public function VideoLoader() 
		{
			vidLoader = new Loader();
			configLoader(imgLoader.contentLoaderInfo);
		}
		
		/** Public methods **/
		public function load() : void
		{
			if (imgLoader != null)
			{
				if (!_multiLoad)
					loadImage( req );
				else
				{
					_imgs = new Vector.<Bitmap>();
					_queue = _requests;
					loadImage( _queue.shift() );
				}
			}
		}
		
		/** Getters & Setters **/		
		public function set url( value:String ) : void
		{
			req = new URLRequest( value );			
		}
		
		public function set request( value:URLRequest ) : void
		{
			req = value;
		}
		
		public function get image() : Bitmap
		{
			return _img;
		}
		public function get images() : Vector.<Bitmap>
		{
			return _imgs;
		}
		
		public function get multiLoad() : Boolean
		{
			return _multiLoad;
		}
		public function set multiLoad( value:Boolean ) : void
		{
			_multiLoad = value;
		}
		
		public function set urls( value:Vector.<String> ) : void
		{
			_multiLoad = true;
			_requests = new Vector.<URLRequest>();
			for (var i:int = 0; i < value.length; i++)
			{
				var url:String = value[i].toString();
				if (url != null)
				{
					var request:URLRequest = new URLRequest( url );
					_requests.push(request);
				}
			}
		}
		
		public function set requests( value:Vector.<URLRequest> ) : void
		{
			_multiLoad = true;
			_requests = value;
		}
		
		/** Private methods **/
		private function loadImage( request:URLRequest ) : void
		{
			imgLoader.load( request );
		}
		
		private function configLoader( d:LoaderInfo ) : void
		{
			d.addEventListener(Event.OPEN, openHandler);
			d.addEventListener(Event.COMPLETE, completeHandler);
			d.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			d.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
			d.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			d.addEventListener(ProgressEvent.PROGRESS, progressHandler);
		}
		
		private function openHandler( e:Event ) : void
		{
			trace("Open: " + e);
			if (hasEventListener(Event.OPEN))
			{
				dispatchEvent(e);
			}
		}
		private function completeHandler( e:Event ) : void
		{
			loaderInfo = e.currentTarget as LoaderInfo;
			
			_img = loaderInfo.content as Bitmap;
			if (_multiLoad)
			{
				_imgs.push(_img);
				if (_queue.length > 0) 
				{
					loadImage( _queue.shift() );
				}
				else
				{
					dispatchEvent(e);
				}
			}
			else 
			{
				dispatchEvent(e);
			}
		}
		private function httpStatusHandler( e:HTTPStatusEvent ) : void
		{
			trace("httpstatus: " + e);
			if (hasEventListener(HTTPStatusEvent.HTTP_STATUS))
			{
				dispatchEvent(e);
			}
		}
		private function errorHandler( e:IOErrorEvent ) : void
		{
			trace("IO Error: " + e);
			if (hasEventListener(IOErrorEvent.IO_ERROR))
			{
				dispatchEvent(e);
			}
		}
		private function securityErrorHandler( e:SecurityErrorEvent ) : void
		{
			trace("security error: " + e);
			if (hasEventListener(SecurityErrorEvent.SECURITY_ERROR))
			{
				dispatchEvent(e);
			}
		}
		private function progressHandler( e:ProgressEvent ) : void
		{
			if (hasEventListener(ProgressEvent.PROGRESS))
			{
				dispatchEvent(e);
			}
		}
	}
	
}